package com.itfreer.print.toExcel;

import java.util.Scanner;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;

import com.itfreer.print.TemplateFilePara;
import com.itfreer.print.toExcel.HtmlToExcelImp;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 定义html转为excel 基于poi重点解决html中的table元素
 */
@Component
public class HtmlToExcelImp implements HtmlToExcel {

	protected final Logger logger = LoggerFactory.getLogger(getClass());
	
	@Autowired(required = false)
	private TemplateFilePara templateFilePara;

	/**
	 * 将html文件转换为excel文件
	 * 
	 * @param htmlFile
	 *            html文件
	 * @param excelFile
	 *            excel文件
	 * @throws IOException
	 */
	public void toExcel(InputStream htmlFile, OutputStream excelFile) throws Exception {
		StringBuilder html = new StringBuilder();
		Scanner s = new Scanner(htmlFile, "utf-8");
		while (s.hasNext()) {
			html.append(s.nextLine());
		}
		s.close();
		HtmlToExcelBase toExcelBase = new HtmlToExcelBase();
		String htmlString = html.toString();
		logger.debug("转换html内容为："+htmlString);
		toExcelBase.doProcess(htmlString, excelFile);
		excelFile.close();
	}

	/**
	 * 基于模板输出excel文件
	 * 
	 * @param tempPath
	 *            模板文件路径
	 * @param templateFile
	 *            模板文件
	 * @param data
	 *            模板数据
	 * @param excelFile
	 *            输出excel文件
	 * @throws IOException
	 * @throws TemplateException
	 */
	@SuppressWarnings("deprecation")
	public void toExcel(String tempPath, String templateFile, Object data, OutputStream excelFile)
			throws IOException, TemplateException {
		ByteArrayOutputStream htmlOut = new ByteArrayOutputStream();
		OutputStreamWriter htmlWriter = new OutputStreamWriter(htmlOut, "UTF-8");

		try {
			Configuration freeMarkerconfig = new Configuration();
			freeMarkerconfig.setDirectoryForTemplateLoading(new File(tempPath));
			freeMarkerconfig.setDefaultEncoding("UTF-8");
			Template freeMarkerTemplateerconfig;
			freeMarkerTemplateerconfig = freeMarkerconfig.getTemplate(templateFile, "UTF-8");
			// 往模板中写入数据
			freeMarkerTemplateerconfig.process(data, htmlWriter);
			htmlWriter.flush();
			htmlOut.flush();

			// html流
			ByteArrayInputStream htmlFile = new ByteArrayInputStream(htmlOut.toByteArray());
			toExcel(htmlFile, excelFile);

			htmlFile.close();
			htmlWriter.close();
			htmlOut.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void toExcel(String templateFile, Object data, OutputStream excelFile) throws Exception {
		if (templateFilePara == null) {
			return;
		}
		
		String tempPath = templateFilePara.getTemplatePath();
		if(!tempPath.contains(":\\") && !tempPath.startsWith("/")) {
			tempPath = this.getClass().getResource("/").getPath() + tempPath;
		}

		toExcel(tempPath, templateFile, data, excelFile);
	}
}
